<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Album\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\ResultSet\ResultSet;
use Zend\Paginator\Paginator;
class AlbumTable extends BaseModel{
    protected  $tableGetway;
    protected $serviceLocator;
   
    public function __construct(TableGateway $tableGetway) {
        $this->tableGetway = $tableGetway;
    }
    public function fetchAll($paginated =false){
         if ($paginated) {
             // create a new Select object for the table album
             $select = new Select('album');
             // create a new result set based on the Album entity
             $resultSetPrototype = new ResultSet();
             $resultSetPrototype->setArrayObjectPrototype(new Album());
             // create a new pagination adapter object
             $paginatorAdapter = new DbSelect(
                 // our configured select object
                 $select,
                 // the adapter to run it against
                 $this->getAdapter(),
                 // the result set to hydrate
                 $resultSetPrototype
             );
             $paginator = new Paginator($paginatorAdapter);
             return $paginator;
         }
        $resultSet = $this->tableGetway->select();
        return $resultSet;
    }
    public function getAlbum($id){
        $id = (int)$id;
        $rowset = $this->tableGetway->select(array('id'=>$id));
        $row    = $rowset->current();
        if(!$row){
            throw new \Exception("Could not find this $id");
        }
        return $row;
    }
    public function saveAlbum(Album $album){
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );
        $id = (int)$album->id;
        if($id == 0){
            $this->tableGetway->insert($data);
        }else{
            if($this->getAlbum($id)){
                $this->tableGetway->update($data,array('id'=>$id));
            }else{
                throw new \Exception("Album does not exist");
            }
        }     
    }
    public function deleteAlbum($id){
        $this->tableGetway->delete(array('id'=>$id));
    }
    public function getArrayOptions(){
        return array(
            '1' => 'Male',
            '2' => 'Female'
        );
    } 
     
    function getLista(){
         $db = $this->getAdapter();// use this method required declare function getAdapter();//line bottom
         $sql ="select * from album";
       // $db = new BaseModel();
       // $db->fetchAllString($sql);
        //$db = $this->getServiceLocator()->get('db_adapter');//get connect info ussing serviceLocator
       
        //$sql ="select * from album";
        
        $list = $db->getDriver()->getConnection()->execute($sql);        //excute query
        $a =array();
        //foreach to list array
        if($list->count() > 0){
            $results = new ResultSet();
            $a= $results->initialize($list)->toArray();
        }
        return $a;
        
    }
}